package com.qiaofang.collectmanage.task;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.logger.Logger;
import com.github.ltsopensource.core.logger.LoggerFactory;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.qiaofang.collectmanage.service.CrawlerTaskService;
import com.qiaofang.collectmanage.stub.bean.CrawlerTaskDTO;
import com.qiaofang.collectmanage.stub.bean.ListCrawlerTaskDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class CrawlerTaskCollectRuleCacheRunner implements JobRunner{

    private final Logger logger = LoggerFactory.getLogger(CrawlerTaskCollectRuleCacheRunner.class);

    @Autowired
    CrawlerTaskService crawlerTaskService;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        try {

            List<CrawlerTaskDTO>  crawlerTaskList = crawlerTaskService.listCrawlerTask(new ListCrawlerTaskDTO());
            List<Integer> ids = crawlerTaskList.stream().map(CrawlerTaskDTO::getId).collect(Collectors.toList());
            if(ids!=null&&!ids.isEmpty()){
                //删除缓存任务频道数据
                crawlerTaskService.clearCrawlerCollectCache(ids);
                crawlerTaskService.listCrawlerCollectRuleByIds(ids);
            }
            logger.info("刷新任务缓存CrawlerCollectRule 时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"ids"+ids);
        }catch(Exception e){
            e.printStackTrace();
        }
        return new Result(Action.EXECUTE_SUCCESS, this.getClass() + " SUCCESS");
    }

}
